define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'company/company_position/index' + location.search,
                    add_url: 'company/company_position/add',
                    edit_url: 'company/company_position/edit',
                    del_url: 'company/company_position/del',
                    multi_url: 'company/company_position/multi',
                    import_url: 'company/company_position/import',
                    table: 'company_position',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='company_id']", form)
                    .addClass("selectpage")
                    .data("source", "company/company/index")
                    .data("primaryKey", "id")
                    .data("orderBy", "id desc");
                $("input[name='region_ids']", form)
                    .addClass("selectpage")
                    .data("source", "region/index")
                    .data("primaryKey", "id")
                    .data("orderBy", "id desc");
                $("input[name='industry_id']", form)
                    .addClass("selectpage")
                    .data("source", "industry/index")
                    .data("primaryKey", "id")
                    .data("orderBy", "id desc");
                $("input[name='position_id']", form)
                    .addClass("selectpage")
                    .data("source", "position/index")
                    .data("primaryKey", "id")
                    .data("orderBy", "id desc");
                $("input[name='worktype_id']", form)
                    .addClass("selectpage")
                    .data("source", "worktype/index")
                    .data("primaryKey", "id")
                    .data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'company_id', title: __('Company_id'), visible: false},
                        {field: 'company.name', title: __('Company_id'), operate: false},
                        {field: 'region_ids', title: __('Region_ids'), visible: false, operate: 'LIKE %...%'},
                        {field: 'region', title: __('Region_ids'), operate: false},
                        // {field: 'industry_id', title: __('Industry_id'), visible: false},
                        // {field: 'industry.name', title: __('Industry_id'), operate: false},
                        // {field: 'position_id', title: __('Position_id'), visible: false},
                        // {field: 'position.name', title: __('Position_id'), operate: false},
                        {field: 'work_type_id', title: __('Work_type_id'), visible: false},
                        {field: 'worktype.name', title: __('Work_type_id'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'tag', title: __('Tag'), operate: 'LIKE', formatter: Table.api.formatter.flag},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'file', title: __('File'), operate: false, formatter: Table.api.formatter.file},
                        {field: 'num', title: __('Num')},
                        {field: 'min_money', title: __('Min_money')},
                        {field: 'max_money', title: __('Max_money')},
                        {field: 'money_date', title: __('Money_date')},
                        {field: 'limit_condition', title: __('Limit_condition'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
